package com.example.threaddemo;

import java.util.concurrent.atomic.AtomicInteger;

public class CASDemo {
    public static void main(String[] args) {
        //从主内存中取值，5
        AtomicInteger atomicInteger = new AtomicInteger(5);
        atomicInteger.compareAndSet(5, 10);
        //如果当前和值和之前的值一样，则允许修改
        System.out.println(atomicInteger.compareAndSet(5, 10) + "  value:" + atomicInteger.get());
        //如果当前和值和之前的值不同，则修改失败，类似git 提交失败
        System.out.println(atomicInteger.compareAndSet(5, 15) + "  value:" + atomicInteger.get());
    }
}
